﻿using System.Linq;
using Wslzy.Auxiliary.Web.Domains.Models;
using Wslzy.Auxiliary.Web.Domains.Repositories;
using Wslzy.Auxiliary.Web.Services.Interface;
using Wslzy.Auxiliary.Infrastructure.Maps;
using Wslzy.Auxiliary.Web.ViewModels;
using Wslzy.Auxiliary.Infrastructure;
using System.Collections.Generic;
using Wslzy.Auxiliary.Infrastructure.Helpers;
using System;
using System.Text;
using Wslzy.Auxiliary.Infrastructure.Datas.UnitOfWorks;

namespace Wslzy.Auxiliary.Web.Services.Implement
{
    public class AccountService : IAccountService
    {
        private IAccountRepository repository;
        private IUnitOfWorkManager workManager;
        public AccountService(IAccountRepository repository,IUnitOfWorkManager workManager)
        {
            this.repository = repository;
            this.workManager = workManager;
        }

        public ResultMsg<string> AddAccount(AccountVM accountVM)
        {
            Account account = new Account(Id.ObjectId());
            accountVM.Id = account.Id;
            account = accountVM.MapTo(account);
            account.Enabled = true;
            account.IsDeleted = false;
            account.Version = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString());
            account.CreationTime = DateTime.Now;
            account.LastModificationTime = DateTime.Now;
            repository.Add(account);
            workManager.Commit();
            var result = new ResultMsg<string>();
            result.IsSuccess = true;
            result.Data = account.Id;
            return result;
        }

        public ResultMsg<List<AccountVM>> GetAccounts()
        {
            var accounts = repository.FindAllNoTracking().ToList();
            var accountvms = accounts.MapToList<AccountVM>();
            var result = new ResultMsg<List<AccountVM>>();
            result.IsSuccess = true;
            result.Data = accountvms;
            return result;
        }
    }
}
